﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApplication3;
using System.Text.RegularExpressions;

namespace bsy
{
    public partial class Employee : Form
    {
        public Employee()
        {
            InitializeComponent();
        }

        private void Employee_Load(object sender, EventArgs e)
        {
            try
            {
                // TODO: 这行代码将数据加载到表“ds_employee.EMP”中。您可以根据需要移动或删除它。
                this.eMPTableAdapter.Fill(this.ds_employee.EMP);
                this.WindowState = FormWindowState.Maximized;

                if (dataGridView.RowCount > 0)
                {
                    Showempdetail(0);
                }
                else
                {
                    Btn_empdelete.Enabled = false;
                    Btn_empedit.Enabled = false;
                }
            }
            catch (Exception E)
            {
                MessageBox.Show("数据库错误！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //取消按钮
        private void Btn_empcancel_Click(object sender, EventArgs e)
        {
            DisableControl();
            Btn_empedit.Visible = true;
            Btn_empadd.Visible = true;
            Btn_empcancel.Visible = false;
            Btn_empsave.Visible = false;
            Btn_addemp.Visible = false;

            int row = -1;
            if (!isfind)
            {
                if (dataGridView.RowCount > 0)
                    row = dataGridView.CurrentCell.RowIndex;
                else
                {
                    Btn_empdelete.Enabled = false;
                    Btn_empedit.Enabled = false;
                }
            }
            else
                row = findrow;

            if(-1 != row)
                Showempdetail(row);
            dataGridView.Enabled = true;
            Btn_empdelete.Visible = true;
        }

        //把控件设为可编辑
        private void EnableControl()
        {
            Tb_empnumber.Enabled = true;
            Tb_empname.Enabled = true;
            Tb_emponame.Enabled = true;
            dtp_addtime.Enabled = true;
            Cb_empsex.Enabled = true;
            Dtp_empbirthday.Enabled = true;
            Tb_emprace.Enabled = true;
            Tb_empaddress.Enabled = true;
            Tb_empic.Enabled = true;
            Cb_empm.Enabled = true;
            Cb_emped.Enabled = true;
            Tb_emphigh.Enabled = true;
            Tb_empweight.Enabled = true;
            Tb_empphone.Enabled = true;
            Tb_empemail.Enabled = true;
            Tb_emppost.Enabled = true;
            Tb_emphp.Enabled = true;
            Tb_empgs.Enabled = true;
            Tb_empsp.Enabled = true;
            Tb_empill.Enabled = true;
            Tb_empao.Enabled = true;
        }

        //把控件设为不可编辑
        private void DisableControl()
        {
            Tb_empnumber.Enabled = false;
            Tb_empname.Enabled = false;
            Tb_emponame.Enabled = false;
            dtp_addtime.Enabled = false;
            Cb_empsex.Enabled = false;
            Dtp_empbirthday.Enabled = false;
            Tb_emprace.Enabled = false;
            Tb_empaddress.Enabled = false;
            Tb_empic.Enabled = false;
            Cb_empm.Enabled = false;
            Cb_emped.Enabled = false;
            Tb_emphigh.Enabled = false;
            Tb_empweight.Enabled = false;
            Tb_empphone.Enabled = false;
            Tb_empemail.Enabled = false;
            Tb_emppost.Enabled = false;
            Tb_emphp.Enabled = false;
            Tb_empgs.Enabled = false;
            Tb_empsp.Enabled = false;
            Tb_empill.Enabled = false;
            Tb_empao.Enabled = false;
        }

        //编辑员工按钮
        private void Btn_empedit_Click(object sender, EventArgs e)
        {
            dataGridView.Enabled = false;
            Btn_empdelete.Visible = false;

            EnableControl();
            Btn_empedit.Visible = false;
            Btn_empadd.Visible = false;
            Btn_empcancel.Visible = true;
            Btn_empsave.Visible = true;
        }

        //增加员工按钮
        private void Btn_empadd_Click(object sender, EventArgs e)
        {
            dataGridView.Enabled = false;
            Btn_empdelete.Visible = false;

            Tb_empnumber.Text = "";
            Tb_empname.Text = "";
            Tb_emponame.Text = "";
            Tb_emprace.Text = "";
            Tb_empaddress.Text = "";
            Tb_empic.Text = "";
            Tb_emphigh.Text = "";
            Tb_empweight.Text = "";
            Tb_empphone.Text = "";
            Tb_empemail.Text = "";
            Tb_emppost.Text = "";
            Tb_emphp.Text = "";
            Tb_empgs.Text = "";
            Tb_empsp.Text = "";
            Tb_empill.Text = "";
            Tb_empao.Text = "";
            Cb_empsex.Text = "";
            Cb_empm.Text = "";
            Cb_emped.Text = "";
            dtp_addtime.Value = DateTime.Now;
            Dtp_empbirthday.Value = DateTime.Now;

            Btn_empedit.Visible = false;
            Btn_empadd.Visible = false;

            Btn_empcancel.Visible = true;
            Btn_empsave.Visible = false;
            Btn_addemp.Visible = true;

            EnableControl();
        }

        //增加员工按钮
        private void Btn_addemp_Click(object sender, EventArgs e)
        {
            Btn_addemp.Enabled = false;

            string name;
            string number;
            string byname;
            string addtime;
            string gender;
            string birthday;
            string nationality;
            string birth_place;
            string IDcard;
            string marrige;
            string edu_bg;
            float stature = 0;
            float weight = 0;
            string phone;
            string email;
            string post_code;
            string tel;
            string school;
            string strong_suit;
            string medical_record;
            string dep_opinion;

            name = Tb_empname.Text.Trim();//获取控件的值
            number = Tb_empnumber.Text.Trim();
            byname = Tb_emponame.Text.Trim();
            addtime = dtp_addtime.Value.ToString();
            gender = Cb_empsex.Text;
            birthday = Dtp_empbirthday.Value.ToString();
            nationality = Tb_emprace.Text.Trim();
            birth_place = Tb_empaddress.Text.Trim();
            IDcard = Tb_empic.Text.Trim();
            marrige = Cb_empm.Text;
            edu_bg = Cb_emped.Text;
            if (Tb_emphigh.Text != string.Empty)
                stature = float.Parse(Tb_emphigh.Text.Trim());
            if (Tb_empweight.Text != string.Empty)
                weight = float.Parse(Tb_empweight.Text.Trim());
            phone = Tb_empphone.Text.Trim();
            email = Tb_empemail.Text.Trim();
            post_code = Tb_emppost.Text.Trim();
            tel = Tb_emphp.Text.Trim();
            school = Tb_empgs.Text.Trim();
            strong_suit = Tb_empsp.Text.Trim();
            medical_record = Tb_empill.Text.Trim();
            dep_opinion = Tb_empao.Text.Trim();

            //插入语句
            string sql = string.Format("insert into EMP(name, munber, byname, begin_time, gender, birthday, nationality, birth_place, IDcard, marriage, edu_bg, stature, weight, phone, email, post_code, tel, school, strong_suit, medical_record, dep_opinion)values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}','{19}','{20}')",
                name, number, byname, addtime, gender, birthday, nationality, birth_place, IDcard, marrige, edu_bg, stature, weight, phone, email, post_code, tel, school, strong_suit, medical_record, dep_opinion);

            //执行并返回受影响行数
            int temp = 0;
            if ("".Equals(Tb_empnumber.Text.ToString()) | "".Equals(Tb_empname.Text.ToString()))
            {
                MessageBox.Show("员工编号或姓名不为空！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Btn_addemp.Enabled = true;
            }
            else if(Empnumber_Isexit())
            {
                Btn_addemp.Enabled = true;
            }
            else
            {
                try
                {
                    temp = CommonDB.ExecuteSql(sql);
                    if (temp == 1)
                    {
                        MessageBox.Show("添加成功！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                    Employee emp = new Employee();
                    emp.MdiParent = this.MdiParent;
                    emp.Show();
                    this.Close();
                }
                catch (Exception E)
                {
                    MessageBox.Show("数据错误，请检查后重试！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    Btn_addemp.Enabled = true;
                }
            }
        }

        //单击datagridview中的员工，显示员工的详细信息
        private void dataGridView_Click(object sender, EventArgs e)
        {
            if (dataGridView.RowCount > 0 && !isfind)
            {
               int row = dataGridView.CurrentCell.RowIndex; 

                Showempdetail(row);

                showYejixiangqing(row);
            }
        }


        //修改员工信息
        private void Btn_empsave_Click(object sender, EventArgs e)
        {
            Btn_empsave.Enabled = false;

            int row;
            if (!isfind)
            {
               row = dataGridView.CurrentCell.RowIndex;
            }
            else
                row = findrow;

            string name;
            string number;
            string byname;
            string gender;
            string birthday;
            string nationality;
            string birth_place;
            string IDcard;
            string marrige;
            string edu_bg;
            float stature;
            float weight;
            string phone;
            string email;
            string post_code;
            string tel;
            string school;
            string strong_suit;
            string medical_record;
            string dep_opinion;

            name = Tb_empname.Text.Trim();
            number = Tb_empnumber.Text.Trim();
            byname = Tb_emponame.Text.Trim();
            gender = Cb_empsex.Text;
            birthday = Dtp_empbirthday.Value.ToString();
            nationality = Tb_emprace.Text.Trim();
            birth_place = Tb_empaddress.Text.Trim();
            IDcard = Tb_empic.Text.Trim();
            marrige = Cb_empm.Text;
            edu_bg = Cb_emped.Text;
            if (Tb_emphigh.Text != string.Empty)
                stature = float.Parse(Tb_emphigh.Text.Trim());
            else
                stature = 0;
            if (Tb_empweight.Text != string.Empty)
                weight = float.Parse(Tb_empweight.Text.Trim());
            else
                weight = 0;
            phone = Tb_empphone.Text.Trim();
            email = Tb_empemail.Text.Trim();
            post_code = Tb_emppost.Text.Trim();
            tel = Tb_emphp.Text.Trim();
            school = Tb_empgs.Text.Trim();
            strong_suit = Tb_empsp.Text.Trim();
            medical_record = Tb_empill.Text.Trim();
            dep_opinion = Tb_empao.Text.Trim();

            string sql = string.Format("update EMP set name='{0}', munber='{1}', byname='{2}', begin_time='{3}', gender='{4}', birthday='{5}', nationality='{6}', birth_place='{7}', IDcard='{8}', marriage='{9}', edu_bg='{10}', stature='{11}', weight='{12}', phone='{13}', email='{14}', post_code='{15}', tel='{16}', school='{17}', strong_suit='{18}', medical_record='{19}', dep_opinion='{20}' where EMP_id='{21}'",
                name, number, byname, DateTime.Now, gender, birthday, nationality, birth_place, IDcard, marrige, edu_bg, stature, weight, phone, email, post_code, tel, school, strong_suit, medical_record, dep_opinion, Convert.ToDouble(dataGridView.Rows[row].Cells[0].Value));

            int temp = 0;
            if ("".Equals(Tb_empnumber.Text.ToString()) | "".Equals(Tb_empname.Text.ToString()))
            {
                MessageBox.Show("员工编号或姓名不为空", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Btn_empsave.Enabled = true;
            }
            else
            {
                try
                {
                    temp = CommonDB.ExecuteSql(sql);

                    if (temp == 1)
                    {
                        MessageBox.Show("修改成功！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }


                    Employee emp = new Employee();
                    emp.MdiParent = this.MdiParent;
                    emp.Show();
                    this.Close();
                }
                catch (Exception E)
                {
                    MessageBox.Show("数据错误，请检查数据后重试！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Btn_empsave.Enabled = true;
                }
            }
        }

        //将员工的详细信息绑定到控件
        private void Showempdetail(int row)
        {
            Tb_empnumber.Text = dataGridView.Rows[row].Cells[1].Value.ToString();
            Tb_empname.Text = dataGridView.Rows[row].Cells[2].Value.ToString();
            Tb_emponame.Text = dataGridView.Rows[row].Cells[3].Value.ToString();
            dtp_addtime.Value = Convert.ToDateTime(dataGridView.Rows[row].Cells[4].Value.ToString());
            Lb_empaddt.Visible = true;

            //if (Cb_empsex.Text != string.Empty)
                Cb_empsex.Text = dataGridView.Rows[row].Cells[5].Value.ToString();
            //else
            //    Cb_empsex.Text = "";

            Dtp_empbirthday.Value = Convert.ToDateTime(dataGridView.Rows[row].Cells[6].Value.ToString());
            Tb_emprace.Text = dataGridView.Rows[row].Cells[7].Value.ToString();
            Tb_empaddress.Text = dataGridView.Rows[row].Cells[8].Value.ToString();
            Tb_empic.Text = dataGridView.Rows[row].Cells[9].Value.ToString();
            Cb_empm.Text = dataGridView.Rows[row].Cells[10].Value.ToString();
            Cb_emped.Text = dataGridView.Rows[row].Cells[11].Value.ToString();

            if (Convert.ToDouble(dataGridView.Rows[row].Cells[12].Value) == 0)
               Tb_emphigh.Text = ""; 
            else
                Tb_emphigh.Text = dataGridView.Rows[row].Cells[12].Value.ToString();

            if (Convert.ToDouble(dataGridView.Rows[row].Cells[13].Value) == 0)
                Tb_empweight.Text = "";
            else
                Tb_empweight.Text = dataGridView.Rows[row].Cells[13].Value.ToString();

            Tb_empphone.Text = dataGridView.Rows[row].Cells[14].Value.ToString();
            Tb_empemail.Text = dataGridView.Rows[row].Cells[15].Value.ToString();
            Tb_emppost.Text = dataGridView.Rows[row].Cells[16].Value.ToString();
            Tb_emphp.Text = dataGridView.Rows[row].Cells[17].Value.ToString();
            Tb_empgs.Text = dataGridView.Rows[row].Cells[18].Value.ToString();
            Tb_empsp.Text = dataGridView.Rows[row].Cells[19].Value.ToString();
            Tb_empill.Text = dataGridView.Rows[row].Cells[20].Value.ToString();
            Tb_empao.Text = dataGridView.Rows[row].Cells[21].Value.ToString();
        }

        //删除员工
        private void Btn_empdelete_Click(object sender, EventArgs e)
        {
            Btn_empdelete.Enabled = false;

            int row;
            if (!isfind)
            {
                row = dataGridView.CurrentCell.RowIndex;
            }
            else
                row = findrow;

            if (MessageBox.Show("确定删除该员工？", "系统提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                string sql = string.Format("delete from EMP where EMP_ID =" + dataGridView.Rows[row].Cells[0].Value.ToString());

                int temp;
                try
                {
                    temp = CommonDB.ExecuteSql(sql);

                    if (temp == 1)
                    {
                        MessageBox.Show("删除成功！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                    Employee emp = new Employee();
                    emp.MdiParent = this.MdiParent;
                    emp.Show();
                    this.Close();
                }
                catch (Exception E)
                {
                    MessageBox.Show("数据库错误，删除失败！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            Btn_empdelete.Enabled = true;
        }

        //进入计算提成项目卡时的事件
        private void tabPage3_Enter(object sender, EventArgs e)//点击提成选项卡时计算提成
        {
            Btn_empcancel_Click(this, null);
            
            dateTimePicker1.Value = DateTime.Now.AddMonths(-1);
            dateTimePicker2.Value = DateTime.Now;

            calculateTicheng();

            try
            {
                //计算营业额
                string sql_pay = string.Format("select sum(pay) from orders where (time between '{0}' and '{1}')", dateTimePicker1.Value, dateTimePicker2.Value);
                DataSet ds = new DataSet();
                ds = CommonDB.Query(sql_pay);

                label4.Text = ds.Tables[0].Rows[0][0].ToString();
                ds.Dispose();
            }
            catch (Exception E)
            {
                MessageBox.Show("数据库错误！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        //电子邮箱检验
        private void Tb_empemail_Validated(object sender, EventArgs e)
        {
            Regex rx = new Regex("\\w{1,}@\\w{1,}\\.\\w{1,}");
            Match m = rx.Match(Tb_empemail.Text.ToString());
            if (!"".Equals(Tb_empemail.Text.ToString()) && !m.Success )
            {
                MessageBox.Show("邮箱错误！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Tb_empemail.Text = "";
            }
        }

        //增加员工时检查员工编号是否唯一
        private bool Empnumber_Isexit()
        {
            bool isexit = false ;
            int row;
            string find = Tb_empnumber.Text.Trim();
            for (row = 0; row < dataGridView.Rows.Count; row++)
            {
                if (find.Equals(dataGridView.Rows[row].Cells[1].Value.ToString()))
                {
                    MessageBox.Show("员工编号已存在，请输入另一编号！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    Tb_empnumber.Text = "";
                    isexit = true;
                    break;
                }
            }
            return isexit;
        }

        //增加员工时,员工身高检验
        private void Tb_emphigh_Validated(object sender, EventArgs e)
        {
            Regex rx = new Regex(@"^\d{2,3}[\.]{0,1}\d{0,2}$");
            Match m = rx.Match(Tb_emphigh.Text.ToString());
            if (!"".Equals(Tb_emphigh.Text.ToString()) && !m.Success)
            {
                MessageBox.Show("身高错误(只能精确到小数点后两位，可为空)！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Tb_emphigh.Text = "";
            }
        }

        private void Tb_empweight_Validated(object sender, EventArgs e)
        {
            Regex rx = new Regex(@"^\d{2,3}[\.]{0,1}\d{0,2}$");
            Match m = rx.Match(Tb_empweight.Text.ToString());
            if (!"".Equals(Tb_empweight.Text.ToString()) && !m.Success)
            {
                MessageBox.Show("体重错误(只能精确到小数点后两位，可为空)！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Tb_empweight.Text = "";
            }
        }

        private void textBox1_Click(object sender, EventArgs e)
        {
            tb_empfind.Text = "";
        }

        bool isfind = false;//设置是否处于查找状态
        int findrow;//保存所找出来的行的行号
        //查找员工按钮
        private void button1_Click(object sender, EventArgs e)
        {
            int row;
            string find = tb_empfind.Text.Trim();
            for (row = 0; row < dataGridView.Rows.Count; row++)
            {
                if (find.Equals(dataGridView.Rows[row].Cells[1].Value.ToString()) | find.Equals(dataGridView.Rows[row].Cells[2].Value.ToString()))
                {
                    break;
                }
            }

            if (row != ds_employee.Tables[0].Rows.Count)
            {
                isfind = true;
                findrow = row;

                CurrencyManager cm = (CurrencyManager)BindingContext[dataGridView.DataSource];
                cm.SuspendBinding();
                for (int i = 0; i < dataGridView.Rows.Count; i++)
                {
                    if (i == row)
                    {
                        dataGridView.Rows[i].Visible = true; 
                        continue;
                    }
                    dataGridView.Rows[i].Visible = false;        
                }
                dataGridView.Rows[row].Selected = true;
                Showempdetail(row);
                cm.ResumeBinding();
                dataGridView.Enabled = false;
            }
            else
            {
                MessageBox.Show("找不到该员工！请您确定是否输入错误", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                tb_empfind.Focus();
                tb_empfind.Text = find;
            }
        }

        //显示所有员工按钮
        private void button2_Click(object sender, EventArgs e)
        {
            //CurrencyManager cm = (CurrencyManager)BindingContext[dataGridView.DataSource];
            //cm.SuspendBinding();
            for (int i = 0; i < dataGridView.Rows.Count; i++)
            {
                dataGridView.Rows[i].Visible = true; 
            }
            dataGridView.Enabled = true ;
            isfind = false;
            tb_empfind.Text = "输入员工编号或姓名查找员工...";
        }

        //计算员工提成按钮
        private void button3_Click(object sender, EventArgs e)
        {
            dataGridView5.DataSource = null;

            calculateTicheng();

            try
            {
                //计算营业额
                string sql_pay = string.Format("select sum(pay) from orders where (time between '{0}' and '{1}')", dateTimePicker1.Value, dateTimePicker2.Value);
                DataSet ds = new DataSet();
                ds = CommonDB.Query(sql_pay);

                label4.Text = ds.Tables[0].Rows[0][0].ToString();
                ds.Dispose();
            }
            catch (Exception E)
            {
                MessageBox.Show("数据库错误！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void calculateTicheng()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("员工编号", typeof(string));
            dt.Columns.Add("员工姓名", typeof(string));
            dt.Columns.Add("护理提成", typeof(float));
            dt.Columns.Add("员工业绩", typeof(float));
            dt.Columns.Add("业绩提成", typeof(float));
            dt.Columns.Add("总提成", typeof(float));

            dataGridView5.DataSource = dt;

            try
            {
                string sql_employee = string.Format("select EMP.EMP_id, EMP.name, EMP.munber from EMP;");//获取员工信息
                DataSet ds_employee = new DataSet();
                ds_employee = CommonDB.Query(sql_employee);

                string sql_deedsinterzone = string.Format("select deedsinterzone.min, deedsinterzone.max, deedsinterzone.scale from deedsinterzone order by deedsinterzone.min;");//获取业绩提成区间表
                DataSet ds_deedsinterzone = new DataSet();
                ds_deedsinterzone = CommonDB.Query(sql_deedsinterzone);

                for (int i = 0; i < ds_employee.Tables[0].Rows.Count; i++)//计算员工提成，并插入员工提成DataGridView
                {
                    float ShouGong = 0;//员工手工提成
                    float YeJi = 0;//员工业绩
                    float YeJiTiCheng = 0;

                    DataRow row = dt.NewRow();

                    row["员工编号"] = ds_employee.Tables[0].Rows[i][2];//写入员工编号
                    row["员工姓名"] = ds_employee.Tables[0].Rows[i][1];//写入员工姓名

                    //取出指定员工在订单表里的记录
                    string sql_emporders = string.Format("select orders.servertype, orders.produceid, orders.price, orders.discount, orders.elevate, orders.achievement from orders where orders.EMP_id='{0}' and (orders.time between '{1}' and '{2}')",
                        ds_employee.Tables[0].Rows[i][0], dateTimePicker1.Value, dateTimePicker2.Value);
                    DataSet ds_emporders = new DataSet();
                    ds_emporders = CommonDB.Query(sql_emporders);

                    for (int j = 0; j < ds_emporders.Tables[0].Rows.Count; j++) //计算员工业绩
                    {
                        if ((ds_emporders.Tables[0].Rows[j][0]).Equals("shougong"))
                            ShouGong += (float)(Convert.ToDouble(ds_emporders.Tables[0].Rows[j][4]));
                        else
                            YeJi += (float)(Convert.ToDouble(ds_emporders.Tables[0].Rows[j][5]));
                    }

                    for (int k = 0; k < ds_deedsinterzone.Tables[0].Rows.Count; k++)//计算员工业绩提成
                    {
                        if (YeJi >= Convert.ToInt32(ds_deedsinterzone.Tables[0].Rows[k][0]) && YeJi <= Convert.ToInt32(ds_deedsinterzone.Tables[0].Rows[k][1]))//处于该区间最小与最大之间
                        {
                            YeJiTiCheng += (YeJi - Convert.ToInt32(ds_deedsinterzone.Tables[0].Rows[k][0])) * (float)(Convert.ToDouble(ds_deedsinterzone.Tables[0].Rows[k][2]));//业绩减去最小乘以比例
                            break;
                        }
                        else
                        {
                            YeJiTiCheng += (Convert.ToInt32(ds_deedsinterzone.Tables[0].Rows[k][1]) - Convert.ToInt32(ds_deedsinterzone.Tables[0].Rows[k][0])) * (float)(Convert.ToDouble(ds_deedsinterzone.Tables[0].Rows[k][2]));//最大减去最小再乘以比例
                        }
                    }

                    row["护理提成"] = ShouGong;//写入员工护理提成
                    row["员工业绩"] = YeJi;//写入员工业绩
                    row["业绩提成"] = YeJiTiCheng;//写入员工业绩提成
                    row["总提成"] = ShouGong + YeJiTiCheng;//写入员工业绩提成

                    dt.Rows.Add(row);

                    ds_emporders.Dispose();
                } 

                ds_employee.Dispose();
                ds_deedsinterzone.Dispose();
            }
            catch (Exception E)
            {
                MessageBox.Show("数据库错误！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        /// <summary>
        /// 查找某个员工业绩详情
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            int row = dataGridView.CurrentCell.RowIndex;
            showYejixiangqing(row);
        }

        //查找某个员工业绩详情
        private void showYejixiangqing(int row)
        {
            if (dataGridView.RowCount > 0)
            {
                try
                {
                    string sql_Yejixiangqing = string.Format("select time as 时间, c_name as 顾客姓名, e_name as 员工姓名, price as 价格, discount as 折扣, elevate as 手工提成, achievement as 业绩, remark as 备注 from orders where EMP_id='{0}' and (time between '{1}' and '{2}')",
                        dataGridView.Rows[row].Cells[0].Value.ToString(), dateTimePicker3.Value, dateTimePicker4.Value);
                    DataSet ds = new DataSet();
                    ds = CommonDB.Query(sql_Yejixiangqing);

                    dataGridView1.DataSource = ds.Tables[0];

                    ds.Dispose();
                }
                catch (Exception E)
                {
                    MessageBox.Show("数据库错误！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        //进入员工业绩详情选项卡时的事件
        private void tabPage2_Enter(object sender, EventArgs e)
        {
            Btn_empcancel_Click(this, null);

            dateTimePicker3.Value = DateTime.Now.AddMonths(-1);
            dateTimePicker4.Value = DateTime.Now; 
        }
    }
}
